package com.feige.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.feige.constant.Constants;
import com.feige.pojo.Clazz;
import com.feige.pojo.User;
import com.feige.pojo.vo.CourseVo;
import com.feige.result.AjaxResult;
import com.feige.result.R;
import com.feige.service.ClazzService;
import com.feige.utils.inventcode.ShareCodeUtil;
import com.feige.utils.redis.RedisUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * <p>
 * 班级表 前端控制器
 * </p>
 *
 * @author feige
 * @since 2020-11-20
 */
@Api(tags = "班级表前端控制器")
@RestController
@RequestMapping("/course/clazz")
public class ClazzController {

    @Resource
    private ClazzService clazzService;


    @Autowired
    private RedisUtils redisUtils;



    @ApiOperation(value = "创建一个班级")
    @PostMapping("/save")
    public AjaxResult save(@Validated @RequestBody Clazz clazz){
        boolean save = clazzService.createClazz(clazz);
        if (save){
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
    }

    @GetMapping("/get/{id}")
    public R<Clazz> get(@PathVariable String id){
        Clazz clazz = clazzService.getClazzById(id);
        if (ObjectUtil.isNotNull(clazz)){
            return R.ok(clazz);
        }
        return R.getInstance(5050,"查询为空",null);
    }

    @GetMapping("/clazz/{id}")
    public AjaxResult getClazzVoById(@PathVariable String id){
        CourseVo clazz = clazzService.getClazzVoById(id);
        if (ObjectUtil.isNotEmpty(clazz)){
            clazz.setInventCode(ShareCodeUtil.toSerialCode(clazz.getInventCode()));
            return AjaxResult.success(clazz);
        }else {
            return AjaxResult.error(5005,"查询为空");
        }
    }

    @GetMapping("/user/{userId}")
    public AjaxResult getClazzByUserId(@PathVariable String userId){
        QueryWrapper<Clazz> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        Clazz clazz = clazzService.getOne(wrapper);
        if (ObjectUtil.isNotEmpty(clazz)){
            clazz.setInventCode(ShareCodeUtil.toSerialCode(clazz.getInventCode()));
            return AjaxResult.success(clazz);
        }else {
            return AjaxResult.success();
        }
    }

    @ApiOperation("通过班级ID查询班级的创建者")
    @GetMapping("/create/user/{clazzId}")
    public AjaxResult getUserByClazzId(@PathVariable String clazzId) {
        User user = clazzService.getUserByClazzId(clazzId);
        return AjaxResult.success(user);
    }

    @GetMapping("/invest/{code}")
    public AjaxResult getClazzByInvestCode(@PathVariable String code){
        CourseVo clazz = clazzService.getClazzByInvestCode(code);
        if (ObjectUtil.isNotEmpty(clazz)){
            clazz.setInventCode(code);
            return AjaxResult.success(clazz);
        }else {
            return AjaxResult.error(5005,"查询为空");
        }
    }

    @ApiOperation(value = "解散一个班级")
    @DeleteMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable String id){
        boolean remove = clazzService.deleteClazzById(id);
        if (remove){
            redisUtils.del(Constants.CLASS_KEY + id);
            return AjaxResult.success();
        }else {
            return AjaxResult.error();
        }
    }

    /**
     * 班级人数加1
     * @param id
     * @return
     */
    @PutMapping("/clazz/incr/{id}")
    public R<Clazz> incrementBy(@PathVariable("id") String id) {
        return R.isOk(clazzService.incrementBy(id));
    }

    /**
     * 班级人数减一
     * @param id
     * @return
     */
    @PutMapping("/clazz/decr/{id}")
    public R<Clazz> decrementBy(@PathVariable("id") String id) {
        return R.isOk(clazzService.decrementBy(id));
    }
}

